home *** CD-ROM | disk | FTP | other *** search
- /*
- File: WATask.c
-
- Contains: a generic task that will provide developers with a starting point
- for writing their own widgets.
-
- Written by: Eric Slosser
-
- Copyright: © 1996 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- */
-
- #include <strings.h>
- #include <stdio.h>
-
- #include <Components.h>
-
- #include "WalkaboutComponents.h"
-
- #define DEBUGLEVEL DEBUGFULL
- #include "Exceptions.h"
-
- //-------------------------------------------------------------------------------------
- // A little cruftiness to allow us to compile under different environments
-
- #if !COMPILING_STANDALONE
- #define WAEnterCodeResource()
- #define WAExitCodeResource()
- #define WAPrepareCodeEntry()
- #else
- #ifdef THINK_C
- #include <SetupA4.h>
- #define WAPrepareCodeEntry() RememberA0()
- #define WAEnterCodeResource() { SetUpA4(); }
- #define WAExitCodeResource() { RestoreA4(); }
- #else
- #ifdef __MWERKS__
- #include <A4Stuff.h>
- #include <SetupA4.h>
- #define WAPrepareCodeEntry() // PrepareCallBack()
- #define WAEnterCodeResource() { EnterCodeResource()
- #define WAExitCodeResource() ExitCodeResource(); }
- #else
- #ifdef __SC__ // MPW
- #define WAPrepareCodeEntry() GlobalWorld saved = GetCurrentGlobalWorld()
- #define WAEnterCodeResource() InitCodeResource()
- #define WAExitCodeResource() SetCurrentGlobalWorld(saved); FreeGlobalWorld()
- #endif
- #endif
- #endif
- #endif
-
- //-------------------------------------------------------------------------------------
- // task specific defines
-
- #define kTaskVersion 0
-
- //-------------------------------------------------------------------------------------
-
- typedef struct { // This is what's contained in the handle that we
- // pass back to the shell.
- Boolean installed;
- Boolean active;
-
- } SettingsRec, **SettingsHandle;
-
- struct Globals { // globals for this task
- Component self;
- short refNum; // the component's resource fork
- };
- typedef struct Globals Globals, **GlobalsHdl;
-
- //-------------------------------------------------------------------------------------
- #pragma mark -
- //-------------------------------------------------------------------------------------
- static OSErr UseSetting( SettingsHandle s, WARebootFlags *flags );
- static OSErr UseSetting( SettingsHandle s, WARebootFlags *flags )
- {
- OSErr err = noErr;
-
- *flags = walkAvailableNow;
-
- return (err);
- } // UseSetting
-
- //-------------------------------------------------------------------------------------
- static OSErr GetCurrentSetting(SettingsHandle s);
- static OSErr GetCurrentSetting(SettingsHandle s)
- {
- OSErr err = noErr;
-
- SetHandleSize((Handle) s, sizeof(SettingsRec));
- if ( err=MemError() ) goto BAIL;
-
- // do your getting here.
-
- BAIL:
- return (err);
- } // GetCurrentSetting
-
-
- //-------------------------------------------------------------------------------------
- // Compare the two settings. Depending on what you store in a SettingsRec, your
- // comparision may be more complex.
-
- static Boolean EqualSettings(SettingsRec *s1, SettingsRec *s2);
- static Boolean EqualSettings(SettingsRec *s1, SettingsRec *s2)
- {
- return ( s1->installed == s2->installed &&
- s1->active == s2->active );
- }
-
- //-------------------------------------------------------------------------------------
- #pragma mark - The rest of the routines are boilerplate for all widgets.
- //-------------------------------------------------------------------------------------
- static pascal ComponentResult GetCurrent( SettingsHandle s );
- static pascal ComponentResult GetCurrent( SettingsHandle s )
- {
- OSErr err;
-
- err = GetCurrentSetting( s );
-
- return err;
- }
-
- //-------------------------------------------------------------------------------------
- static pascal ComponentResult SetCurrent( SettingsHandle new, WARebootFlags *flags);
- static pascal ComponentResult SetCurrent( SettingsHandle new, WARebootFlags *flags)
- {
- ComponentResult err = noErr;
- SettingsHandle cur = nil;
-
- *flags = walkNoChange; // preset to harmless value
-
- cur = (SettingsHandle) NewHandle( sizeof(SettingsRec) );
- GetCurrentSetting( cur );
-
- if (!EqualSettings(*new,*cur))
- {
- err = UseSetting(new,flags);
- }
-
- BAIL:
- return err;
- }
- //-------------------------------------------------------------------------------------
- static pascal ComponentResult CompareSettings( SettingsHandle setting1, SettingsHandle setting2, Boolean *equal );
- static pascal ComponentResult CompareSettings( SettingsHandle setting1, SettingsHandle setting2, Boolean *equal )
- {
- ComponentResult err = noErr;
-
- *equal = EqualSettings( *setting1, *setting2 );
-
- return err;
- }
-
- //-------------------------------------------------------------------------------------
- static pascal ComponentResult DescribeSettings( SettingsHandle s, StringHandle text);
- static pascal ComponentResult DescribeSettings( SettingsHandle s, StringHandle text)
- {
- ComponentResult err = noErr;
- Str255 tmp = "\pdescription not implemented";
-
- PtrToXHand( tmp+1, (Handle) text, *tmp );
-
- return err;
- }
- //-------------------------------------------------------------------------------------
- static pascal ComponentResult DescribeError( OSErr lastErr, Str255 errStr );
- static pascal ComponentResult DescribeError( OSErr lastErr, Str255 errStr )
- {
- ComponentResult err = noErr;
- Str255 tmp = "\pWe don't describe errors yet";
-
- BlockMoveData(tmp, errStr, *tmp+1);
- BAIL:
- return err;
- }
- //-------------------------------------------------------------------------------------
- static pascal ComponentResult Open(ComponentInstance self);
- static pascal ComponentResult Open(ComponentInstance self)
- {
- OSErr err = noErr;
- GlobalsHdl g = (GlobalsHdl) NewHandleClear(sizeof(Globals));
-
- // allocate your widget's storage (if you have any).
- // in the main routine, you'll have to change the call "CallComponentFunction" to
- // "CallComponentFunctionWithStorage", and alter the parameters for all the pascal
- // functions to start with a pointer to your storage.
-
- if (!g) { err = memFullErr; goto BAIL; }
- SetComponentInstanceStorage(self,(Handle) g);
-
- (**g).self = (Component) self;
- (**g).refNum = OpenComponentResFile( (Component) self );
-
- BAIL:
- return err;
- }
- //-------------------------------------------------------------------------------------
- static pascal ComponentResult Close(ComponentInstance self);
- static pascal ComponentResult Close(ComponentInstance self)
- {
- OSErr err = noErr;
- GlobalsHdl g = (GlobalsHdl) GetComponentInstanceStorage(self);
-
- require( g, BAIL );
- if ((**g).refNum>0)
- CloseComponentResFile((**g).refNum);
-
- DisposeHandle((Handle)g);
- SetComponentInstanceStorage(self, nil);
-
- BAIL:
- return err;
- }
-
- //-------------------------------------------------------------------------------------
- #if TASKS_IN_APP
- pascal ComponentResult WATask( ComponentParameters *params, Handle storage );
- pascal ComponentResult WATask( ComponentParameters *params, Handle storage )
- #else
- pascal ComponentResult main( ComponentParameters *params, Handle storage )
- #endif
- {
- ComponentResult result = noErr;
- ComponentFunctionUPP func = nil;
-
- WAPrepareCodeEntry();
- WAEnterCodeResource();
-
- if (params->what<0) // required request codes
- {
- switch (params->what)
- {
- case kComponentOpenSelect :
- func = (ComponentFunctionUPP) Open; break;
-
- case kComponentCloseSelect :
- func = (ComponentFunctionUPP) Close; break;
-
- case kComponentCanDoSelect :
- {
- short selector = *(short*) params->params;
- result = false;
- if ( selector==kWADescribeErrorSelect )
- result = true;
- break;
- }
- case kComponentVersionSelect :
- result = kTaskVersion;
- break;
- case kComponentRegisterSelect :
- result = false; // false means "yes, please register me"
- break;
- case kComponentTargetSelect :
- case kComponentUnregisterSelect :
- result = badComponentSelector;
- break;
- }
- if (func)
- result = CallComponentFunction(params, func);
- }
- else // component specific request codes
- {
- switch (params->what)
- {
- case kWAGetCurrentSelect:
- func = (ComponentFunctionUPP) GetCurrent; break;
-
- case kWASetCurrentSelect:
- func = (ComponentFunctionUPP) SetCurrent; break;
-
- case kWADescribeSettingsSelect:
- func = (ComponentFunctionUPP) DescribeSettings; break;
-
- case kWACompareSettingSelect:
- func = (ComponentFunctionUPP) CompareSettings; break;
-
- case kWADescribeErrorSelect:
- func = (ComponentFunctionUPP) DescribeError; break;
-
- default:
- func = (ComponentFunctionUPP) -1;
- }
-
- if (func == (ComponentFunctionUPP) -1)
- result = badComponentSelector;
- else if (func)
- result = CallComponentFunction(params, func);
-
- }
-
- #if DEBUG
- {
- char str[256];
-
- sprintf( str, "what = %d, result = %ld", params->what, result );
- DebugStr(c2pstr(str));
-
- }
- #endif
-
- WAExitCodeResource();
- return result;
- } // OTTask
- //-------------------------------------------------------------------------------------
-